package zunge.lbe.analysis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import zunge.tools.Tools;

public class Analysis_Social {
	DataSet dataSet;
	String libFolder;
	
	String folder;
	
	public Analysis_Social(DataSet dataSet, String libFolder){
		this.dataSet = dataSet;
		this.libFolder = libFolder;
		this.folder = dataSet.folder + "social/";
	}
	
	public void gen() throws Exception{
		new File(folder).mkdir();
		
		List<String> scripts = new ArrayList<String>();
		
		Irlba irlba_up = new Irlba(folder, "svd_up_global");
		irlba_up.genData(dataSet, dataSet.getFile(dataSet.fsuf_recordUAP), dataSet.getFile(dataSet.fsuf_recordUAP), 
				dataSet.uMap.size(), Irlba.FLAG_UP);
		scripts.add(irlba_up.genScript(libFolder));
		
		Irlba irlba_uap = new Irlba(folder, "svd_uap_global");
		irlba_uap.genData(dataSet, dataSet.getFile(dataSet.fsuf_recordUAP), dataSet.getFile(dataSet.fsuf_recordUAP), 
				dataSet.uMap.size(), Irlba.FLAG_UAP);
		scripts.add(irlba_uap.genScript(libFolder));
		
		for(DataSetFold dataSetFold:dataSet.folds){
			irlba_up = new Irlba(folder, "svd_up_v"+dataSetFold.v);
			irlba_up.genData(dataSet, dataSetFold.getFile(dataSetFold.fsuf_train), dataSet.getFile(dataSet.fsuf_recordUAP), 
					dataSet.uMap.size(), Irlba.FLAG_UP);
			scripts.add(irlba_up.genScript(libFolder));
			
			irlba_uap = new Irlba(folder, "svd_uap_v"+dataSetFold.v);
			irlba_uap.genData(dataSet, dataSetFold.getFile(dataSetFold.fsuf_train), dataSet.getFile(dataSet.fsuf_recordUAP), 
					dataSet.uMap.size(), Irlba.FLAG_UAP);
			scripts.add(irlba_uap.genScript(libFolder));
		}
		
		ScriptMgr.splitScript(scripts, 6, folder+"cmd_social.sh");
	}
	
	public void test() throws Exception{
		List<String> scripts = new ArrayList<String>();
		
		for(DataSetFold dataSetFold:dataSet.folds){
			Irlba irlba_up = new Irlba(folder, "svd_up_v"+dataSetFold.v);
			scripts.add(irlba_up.genTestScript(libFolder, Irlba.FLAG_UP, dataSetFold.getFile(dataSetFold.fsuf_train), dataSetFold.getFile(dataSetFold.fsuf_test)));
			Irlba irlba_uap = new Irlba(folder, "svd_uap_v"+dataSetFold.v);
			scripts.add(irlba_uap.genTestScript(libFolder, Irlba.FLAG_UAP, dataSetFold.getFile(dataSetFold.fsuf_train), dataSetFold.getFile(dataSetFold.fsuf_test)));
		}
		
		ScriptMgr.splitScript(scripts, 10, folder+"cmd_socialtest.sh");
	}
	
	public void collectResult() throws Exception{
		BufferedWriter bw = new BufferedWriter(new FileWriter(folder+"result_social_test.txt"));
		double[] accUp = new double[dataSet.V];
		double[] accUap = new double[dataSet.V];
		for(DataSetFold dataSetFold:dataSet.folds){
			Irlba irlba_up = new Irlba(folder, "svd_up_v"+dataSetFold.v);
			accUp[dataSetFold.v] = irlba_up.getTestAccuracy();
			Irlba irlba_uap = new Irlba(folder, "svd_uap_v"+dataSetFold.v);
			accUap[dataSetFold.v] = irlba_uap.getTestAccuracy();
		}
		bw.write("up "+Tools.getAverage(accUp)+" "+Tools.getVariance(accUp)+"\n");
		bw.write("uap "+Tools.getAverage(accUap)+" "+Tools.getVariance(accUap)+"\n");
		bw.close();
	}

}
